class Admin::PostsController < Admin::AdminApplicationController
  layout "admin"
  before_filter :admin_login_required
  
  def index
    @posts = Post.paginate :page => params[:page], :order => "created_at desc"
  end
  
  def search
    if params[:search_type].nil?
      redirect_back_or_default admin_posts_url
    elsif params[:search_type] == 'id'
      @post = Post.find(:first, :conditions => ["id = ?", params[:q]])
    elsif params[:search_type] == 'content'
      #@posts = Post.find_by_contents(params[:q])
      @posts = Post.paginate_search(params[:q], {:page => params[:page]})
    else
      redirect_back_or_default admin_posts_url
    end
  end
  
  def show
    @post = Post.find(params[:id])
    @replies = @post.replies
  end
  
  def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to admin_posts_url
  end
  
  def edit
        @post = Post.find(params[:id])
        @page_title = ' �༭����'
        @tags = Tag.find_by_sql("select tags.id, tags.name  from taggings, tags, posts  where tags.id = taggings.tag_id and posts.id = #{@post.id} and posts.id = taggings.taggable_id and taggings.taggable_type = 'Post' group by tag_id")
        @tags_text = @tags.collect{|n| n.name}.join(' ')
        if @post.class == CommonPost
            @categories = top_post_category
            firstcat = PostCategory.find(:first, :conditions => [ "id = ?", @post.post_category.parent])
            @first_cat_children  = firstcat.children.map{|u| [u.title, u.id]}
        end
    end
  
    def update
        @post = Post.find(params[:id])
        @tags = Tag.find_by_sql("select tags.id, tags.name  from taggings, tags, posts  where tags.id = taggings.tag_id and posts.id = #{@post.id} and posts.id = taggings.taggable_id and taggings.taggable_type = 'Post' group by tag_id")
          @post.update_attributes(params[:post])
            @post.tag_with(params[:newpost][:tags])
            Tag.parse(params[:newpost][:tags]).each do |name|
                exist_tags = Tag.find_by_sql ["SELECT * FROM tags INNER JOIN user_tags ON tags.id = user_tags.tag_id WHERE tags.name = ? AND user_tags.user_id = ?", name, @post.user.id]
                if exist_tags.empty?
                    tag = Tag.find(:first, :conditions => [ "name = ?", name])
                    user_tag = UserTag.new(:user => @post.user, :tag => tag)
                    user_tag.save!
                end
            end
	  
            new_tags = Array.new
                Tag.parse(params[:newpost][:tags]).each do |name|
                new_tags << name
            end
	  
            now_tags = Array.new
                @tags.each do |t|
                now_tags << t.name
            end
	  
            delete_tags = (new_tags + now_tags).uniq - new_tags  
            delete_tags.each do |name|
                tag = Tag.find(:first, :conditions => [ "name = ?", name])
                user_tag = UserTag.new(:user => @post.user, :tag => tag)
                user_tag.save!
            end
          
            flash[:notice] = '���Ӹ��³ɹ�.'
            if @post.class == CommonPost
                redirect_to admin_common_post_url(@post.community.district, @post.community, @post)
            elsif @post.class == GroupPost
                redirect_to admin_group_post_url(@post.group, @post)
            elsif @post.class == SitePost
                redirect_to admin_site_post_url(@post.site, @post)
            end
    end
    
    def cat_changed
        @category = PostCategory.find(:first, :conditions => [ "id = ?", params[:parent_id]])
        if !@category.nil?
            @children = @category.children.map{|u| [u.title, u.id]}
        end
        render :partial=>'childCat'
    end
end
